package top.songxs.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import top.songxs.entity.*;
import top.songxs.result.Result;
import top.songxs.service.*;
import top.songxs.vo.HouseQueryVo;
import top.songxs.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/house")
public class HouseController {
    @Reference
    private HouseService houseService;

    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo<HouseVo>> list(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody HouseQueryVo houseQueryVo){
        PageInfo<HouseVo> page =  this.houseService.findListPage(pageNum,pageSize,houseQueryVo);
        return Result.ok(page);
    }
    @Reference
    private CommunityService communityService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private UserFollowService userFollowService;
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable Long id, HttpServletRequest request){
        Map map = new HashMap();

        //得到房源消息
        House house = this.houseService.getById(id);
        map.put("house",house);
        //得到小区信息
        Community community = this.communityService.getById(house.getCommunityId());
        map.put("community",community);
        //得到房源的经纪人列表
        List<HouseBroker> houseBrokerList = this.houseBrokerService.findListByHouseId(id);
        map.put("houseBrokerList",houseBrokerList);
        //得到房源的房源图片列表:type=1
        List<HouseImage> houseImage1List = this.houseImageService.findList(id,1);
        map.put("houseImage1List",houseImage1List);
        boolean isFollow = false;
        //必须已经登录，并且当前用户已经关注了房屋
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("USER");

        if(userInfo!=null){
            isFollow = this.userFollowService.isFollow(id,userInfo.getId());
        }
        map.put("isFollow",isFollow);
        return Result.ok(map);
    }

}
